package com.sandking.db.bean;

import com.sandking.tools.SK_Map;
import java.util.ArrayList;
import com.sandking.db.cache.YhCache;
import com.alibaba.fastjson.JSON;
import java.util.Map;
import com.sandking.io.SK_OutputStream;
import java.util.List;
import com.sandking.io.SK_InputStream;
import java.io.ByteArrayInputStream;
import com.sandking.db.cache.YhfbCache;
import java.util.HashMap;
import java.io.ByteArrayOutputStream;

/**
 * 用户副本
 */
public class Yhfb {

	public static final String TABLENAME = "用户副本";
	public static final String CLASSNAME = "Yhfb"; 
	/**
	 * 需要更新字段集
	 */
	private Map<String, Object> updateColumns;

	/** id */
	private int id;
	
	/** 副本id */
	private int fbid;
	
	/** 用户_id */
	private int yh_id;
	
	/** 星数 */
	private int xs;
	
	/** 当天攻击次数 */
	private int dtgjcs;
	
	/** 最后攻击时间 */
	private java.util.Date zhgjsj;
	
	
	public Yhfb() {
		super();
	}
	
	public Yhfb(int id, int fbid, int yh_id, int xs, int dtgjcs, java.util.Date zhgjsj) {
		super();
		this.id = id;
		this.fbid = fbid;
		this.yh_id = yh_id;
		this.xs = xs;
		this.dtgjcs = dtgjcs;
		this.zhgjsj = zhgjsj;
	}
	
	public Map<String, Object> getUpdateColumns() {
		if(updateColumns == null)
			updateColumns = new HashMap<String, Object>();
		return updateColumns;
	}
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
		addUpdateColumn("id",id);
	}
	
	public void changeIdWith(int id){
		this.id += id;
		setId(this.id);
	}
	
	public void changeIdWithMin(int id,int min){
		this.id += id;
		this.id = this.id < min ? min : this.id;
		setId(this.id);
	}
	
	public void changeIdWithMax(int id,int max){
		this.id += id;
		this.id = this.id > max ? max : this.id;
		setId(this.id);
	}
	
	public void changeIdWithMaxMin(int id,int max,int min){
		this.id += id;
		this.id = this.id < min ? min : this.id;
		this.id = this.id > max ? max : this.id;
		setId(this.id);
	}	
	public int getFbid() {
		return fbid;
	}
	
	public void setFbid(int fbid) {
		this.fbid = fbid;
		addUpdateColumn("副本id",fbid);
	}
	
	public void changeFbidWith(int fbid){
		this.fbid += fbid;
		setFbid(this.fbid);
	}
	
	public void changeFbidWithMin(int fbid,int min){
		this.fbid += fbid;
		this.fbid = this.fbid < min ? min : this.fbid;
		setFbid(this.fbid);
	}
	
	public void changeFbidWithMax(int fbid,int max){
		this.fbid += fbid;
		this.fbid = this.fbid > max ? max : this.fbid;
		setFbid(this.fbid);
	}
	
	public void changeFbidWithMaxMin(int fbid,int max,int min){
		this.fbid += fbid;
		this.fbid = this.fbid < min ? min : this.fbid;
		this.fbid = this.fbid > max ? max : this.fbid;
		setFbid(this.fbid);
	}	
	public int getYh_id() {
		return yh_id;
	}
	
	public void setYh_id(int yh_id) {
		this.yh_id = yh_id;
		addUpdateColumn("用户_id",yh_id);
	}
	
	public void changeYh_idWith(int yh_id){
		this.yh_id += yh_id;
		setYh_id(this.yh_id);
	}
	
	public void changeYh_idWithMin(int yh_id,int min){
		this.yh_id += yh_id;
		this.yh_id = this.yh_id < min ? min : this.yh_id;
		setYh_id(this.yh_id);
	}
	
	public void changeYh_idWithMax(int yh_id,int max){
		this.yh_id += yh_id;
		this.yh_id = this.yh_id > max ? max : this.yh_id;
		setYh_id(this.yh_id);
	}
	
	public void changeYh_idWithMaxMin(int yh_id,int max,int min){
		this.yh_id += yh_id;
		this.yh_id = this.yh_id < min ? min : this.yh_id;
		this.yh_id = this.yh_id > max ? max : this.yh_id;
		setYh_id(this.yh_id);
	}	
	public int getXs() {
		return xs;
	}
	
	public void setXs(int xs) {
		this.xs = xs;
		addUpdateColumn("星数",xs);
	}
	
	public void changeXsWith(int xs){
		this.xs += xs;
		setXs(this.xs);
	}
	
	public void changeXsWithMin(int xs,int min){
		this.xs += xs;
		this.xs = this.xs < min ? min : this.xs;
		setXs(this.xs);
	}
	
	public void changeXsWithMax(int xs,int max){
		this.xs += xs;
		this.xs = this.xs > max ? max : this.xs;
		setXs(this.xs);
	}
	
	public void changeXsWithMaxMin(int xs,int max,int min){
		this.xs += xs;
		this.xs = this.xs < min ? min : this.xs;
		this.xs = this.xs > max ? max : this.xs;
		setXs(this.xs);
	}	
	public int getDtgjcs() {
		return dtgjcs;
	}
	
	public void setDtgjcs(int dtgjcs) {
		this.dtgjcs = dtgjcs;
		addUpdateColumn("当天攻击次数",dtgjcs);
	}
	
	public void changeDtgjcsWith(int dtgjcs){
		this.dtgjcs += dtgjcs;
		setDtgjcs(this.dtgjcs);
	}
	
	public void changeDtgjcsWithMin(int dtgjcs,int min){
		this.dtgjcs += dtgjcs;
		this.dtgjcs = this.dtgjcs < min ? min : this.dtgjcs;
		setDtgjcs(this.dtgjcs);
	}
	
	public void changeDtgjcsWithMax(int dtgjcs,int max){
		this.dtgjcs += dtgjcs;
		this.dtgjcs = this.dtgjcs > max ? max : this.dtgjcs;
		setDtgjcs(this.dtgjcs);
	}
	
	public void changeDtgjcsWithMaxMin(int dtgjcs,int max,int min){
		this.dtgjcs += dtgjcs;
		this.dtgjcs = this.dtgjcs < min ? min : this.dtgjcs;
		this.dtgjcs = this.dtgjcs > max ? max : this.dtgjcs;
		setDtgjcs(this.dtgjcs);
	}	
	public java.util.Date getZhgjsj() {
		return zhgjsj;
	}
	
	public void setZhgjsj(java.util.Date zhgjsj) {
		this.zhgjsj = zhgjsj;
		addUpdateColumn("最后攻击时间",zhgjsj);
	}
	
	
	//用户_id
	public Yh getYhPkYh_id(){
		return YhCache.getById(yh_id);
	}
	public void addUpdateColumn(String key, Object val) {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.put(key, val);
	}
	
	public void clearUpdateColumn() {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.clear();
	}
	
	public Map<String, Object> toMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("fbid", this.fbid);
        result.put("yh_id", this.yh_id);
        result.put("xs", this.xs);
        result.put("dtgjcs", this.dtgjcs);
        result.put("zhgjsj", this.zhgjsj);
        return result;
    }
    
    public String toString(){
        return toMap().toString();
    }
    
    public String toJson(){
    	return JSON.toJSONString(toMap());
    }
    
    /**
     * 数据库源字段Map
     */
    public Map<String, Object> toColumnNameMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("副本id", this.fbid);
        result.put("用户_id", this.yh_id);
        result.put("星数", this.xs);
        result.put("当天攻击次数", this.dtgjcs);
        result.put("最后攻击时间", this.zhgjsj);
        return result;
    }
    
    public String toColumnNameString(){
        return toColumnNameMap().toString();
    }
    
    public byte[] toBytes() throws Exception {
    	try (ByteArrayOutputStream out = new ByteArrayOutputStream();) {
		    SK_OutputStream.writeInt(out,this.id);
		    SK_OutputStream.writeInt(out,this.fbid);
		    SK_OutputStream.writeInt(out,this.yh_id);
		    SK_OutputStream.writeInt(out,this.xs);
		    SK_OutputStream.writeInt(out,this.dtgjcs);
		    return out.toByteArray();
    	}catch (Exception e) {
            throw e;
        }
    }
    
     public static Yhfb createForBytes(byte[] _byte) throws Exception {
     	try (ByteArrayInputStream in = new ByteArrayInputStream(_byte);) {
	     	Yhfb yhfb = new Yhfb();
		    yhfb.id = SK_InputStream.readInt(in,null);
		    yhfb.fbid = SK_InputStream.readInt(in,null);
		    yhfb.yh_id = SK_InputStream.readInt(in,null);
		    yhfb.xs = SK_InputStream.readInt(in,null);
		    yhfb.dtgjcs = SK_InputStream.readInt(in,null);
		    return yhfb;
    	}catch (Exception e) {
            throw e;
        }
     }
    
    /**
     * 根据list创建对象
     */
    public static List<Yhfb> createForColumnNameList(List<Map<String, Object>> list){
    	List<Yhfb> yhfbs = new ArrayList<Yhfb>();
		for (Map<String, Object> map : list) {
			yhfbs.add(createForColumnNameMap(map));
		}
		return yhfbs;
    }
    
    /**
     * 根据map创建对象
     */
    public static Yhfb createForColumnNameMap(Map<String, Object> map){
    	Yhfb obj = new Yhfb();
	    obj.id = SK_Map.getInt("id", map);
	    obj.fbid = SK_Map.getInt("副本id", map);
	    obj.yh_id = SK_Map.getInt("用户_id", map);
	    obj.xs = SK_Map.getInt("星数", map);
	    obj.dtgjcs = SK_Map.getInt("当天攻击次数", map);
        return obj;
    }
    
    /**
     * 根据list创建对象
     */
    public static List<Yhfb> createForList(List<Map<String, Object>> list){
    	List<Yhfb> yhfbs = new ArrayList<Yhfb>();
		for (Map<String, Object> map : list) {
			yhfbs.add(createForColumnNameMap(map));
		}
		return yhfbs;
    }
    
    /**
     * 根据map创建对象
     */
    public static Yhfb createForMap(Map<String, Object> map){
    	Yhfb obj = new Yhfb();
	    obj.id = SK_Map.getInt("id", map);
	    obj.fbid = SK_Map.getInt("fbid", map);
	    obj.yh_id = SK_Map.getInt("yh_id", map);
	    obj.xs = SK_Map.getInt("xs", map);
	    obj.dtgjcs = SK_Map.getInt("dtgjcs", map);
        return obj;
    }
    
    public static Yhfb createForJson(String json){
    	Map<String, Object> map = JSON.parseObject(json);
    	return createForMap(map);
    }
    
    public static List<Yhfb> createForJson(List<String> jsons){
    	List<Yhfb> yhfbs = new ArrayList<Yhfb>();
    	for(String json : jsons){
    		yhfbs.add(createForJson(json));
    	}
    	return yhfbs;
    }
    
    /** 延迟插入数据库 */
    public Yhfb insert(){
    	return YhfbCache.insert(this);
    }
    /** 延迟更新数据库 */
    public Yhfb update(){
    	return YhfbCache.update(this);
    }
    /** 延迟删除数据库 */
    public boolean delete(){
    	return YhfbCache.delete(this);
    }
    /** 即时插入数据库 */
    public Yhfb insertAndFlush(){
    	return YhfbCache.insertAndFlush(this);
    }
    /** 即时更新数据库 */
    public Yhfb updateAndFlush(){
    	return YhfbCache.updateAndFlush(this);
    }
    /** 即时删除数据库 */
    public boolean deleteAndFlush(){
    	return YhfbCache.deleteAndFlush(this);
    }
}